/**
 * 
 */
$(function() {
	$("#accordion").accordion();
	$("#orderTime").datetimepicker({
		dateFormat : "yy-mm-dd",
		timeFormat : "HH:mm:ss"
	});
	$("#accordion div li").addClass('myAccordionClass');
	$("#content").append($("#clientListDiv"));
	$("#content").append($("#addClientDiv"));
	$("#content").append($("#orderListDiv"));
	$("#content").append($("#addOrderDiv"));
	$("#content").append($("#clientDetailDiv"));

	$("#accordion div li").unbind().bind('click', function() {
		hideAll();
		var thisId = $(this).attr("id");
		if ('ulist' == thisId) {
			$("#clientListDiv").show();
			refreshClientList();
		} else if ('uadd' == thisId) {
			$("#addClientDiv").show();
		} else if ('olist' == thisId) {
			$("#orderListDiv").show();
			refreshOrderList();
		} else if ('oadd' == thisId) {
			$("#addOrderDiv").show();
			refreshClientOption();
		} else if ('importClient' == thisId) {
			$("#addOrderDiv").show();
			refreshClientOption();
		} else if ('sleasSummary' == thisId) {
			$("#addOrderDiv").show();
			refreshClientOption();
		} else if ('logout' == thisId) {
			logout();
		}
	});

	$("#clientListDiv").show();
	refreshClientList();

	$('#addClientSubmit').unbind().bind('click', function() {
		var clientName = $("#clientName")[0].value;
		if (clientName == "" || clientName == null) {
			alert("please input clientName");
			$("#clientName").focus();
			return false;
		}

		$.getJSON("servlet?method=addClient", {
			'clientName' : clientName,
			'clientType' : $("#clientType")[0].value,
			'mobilePhone1' : $("#mobilePhone1")[0].value,
			'mobilePhone2' : $("#mobilePhone2")[0].value,
			'telephone' : $("#telephone")[0].value,
			'email' : $("#email")[0].value,
			'region' : $("#region")[0].value,
			'profile' : $("#profile")[0].value
		}, function(data) {
			hideAll();
			$("#clientListDiv").show();
			refreshClientList();
			if (data)
				alert(data);
		});
	});

	$('#addOrderSubmit').unbind().bind('click', function() {
		var clientName = $("#addOrderClientName")[0].value;
		var nameOfGoods = $("#nameOfGoods")[0].value;
		var specifications = $("#specifications")[0].value;
		var unitPrice = $("#unitPrice")[0].value;
		var salesNumber = $("#salesNumber")[0].value;
		if (clientName == "" || clientName == null) {
			alert("please input clientName");
			$("#addOrderClientName").focus();
			return false;
		} else if (nameOfGoods == "" || nameOfGoods == null) {
			alert("please input client nameOfGoods");
			$("#nameOfGoods").focus();
			return false;
		} else if (specifications == "" || specifications == null) {
			alert("please input specifications");
			$("#specifications").focus();
			return false;
		} else if (salesNumber == "" || salesNumber == null) {
			alert("please input salesNumber");
			$("#salesNumber").focus();
			return false;
		} else if (unitPrice == "" || unitPrice == null) {
			alert("please input unitPrice");
			$("#unitPrice").focus();
			return false;
		}

		$.getJSON("servlet?method=addOrder", {
			'clientName' : clientName,
			'orderTime' : $("#orderTime")[0].value,
			'nameOfGoods' : nameOfGoods,
			'specifications' : specifications,
			'unitPrice' : unitPrice,
			'salesNumber' : salesNumber,
			'money' : $("#money")[0].value
		}, function(data) {
			hideAll();
			$("#orderListDiv").show();
			refreshOrderList();
			if (data)
				alert(data);
		});
	});
	// $("#addOrderDiv #money").unbind().bind('blur', function() {
	// alert("money")
	// });

});

// free function

function logout() {
	$.getJSON("servlet?method=logout", {
		'p' : '1'
	}, function(data) {
		location.href = '../index.jsp';
	});
};

function refreshClientList() {
	$("#clientListTable tr").eq(0).nextAll().remove();
	$.getJSON("servlet?method=listAllClient", {
		'p' : '1'
	}, function(data) {
		$("#clientListDiv").show();
		if (data) {
			$("#clientListTable").append(data);
		}
	});
};

function refreshClientOption() {
	$.getJSON("servlet?method=getClientNameOption", {
		'p' : '1'
	}, function(data) {
		$("#addOrderClientName option").remove();
		if (data) {
			$("#addOrderClientName").append(data);
		}
	});
};

function refreshOrderList() {
	$("#orderListTable tr").eq(0).nextAll().remove();
	$.getJSON("servlet?method=listAllOrder", {
		'p' : '1'
	}, function(data) {
		if (data) {
			$("#orderListTable").append(data);
		}
	});
};

function payOrder(id) {
	$.getJSON("servlet?method=payOrder", {
		'id' : id
	}, function(data) {
		if (data)
			alert(data);
		refreshOrderList();
	});
};
function cancelPayOrder(id) {
	$.getJSON("servlet?method=cancelPayOrder", {
		'id' : id
	}, function(data) {
		if (data)
			alert(data);
		refreshOrderList();
	});
};
function deleteOrder(id) {
	$.getJSON("servlet?method=deleteOrder", {
		'id' : id
	}, function(data) {
		if (data)
			alert(data);
		refreshOrderList();
	});
};

function clientDetail(id) {
	hideAll();
	$("#clientDetailDiv").show();
};

function deleteClient(id) {
	$.getJSON("servlet?method=deleteClient", {
		'id' : id
	}, function(data) {
		if (data)
			alert(data);
		refreshClientList();
	});
};

function checkAddClient() {
	var clientName = $("#addClientDiv #clientName")[0].value;
	if (clientName == "") {
		alert("please input client Name");
		$("#addClientDiv #clientName").focus()
		return false;
	}
	return true;
}

function validOrderForm() {
	var salesNumber = $("#addOrderDiv #salesNumber")[0].value;
	var unitPrice = $("#addOrderDiv #unitPrice")[0].value;
	var floatReg = /^\d+(\.\d+)?$/;
	if (!floatReg.test(unitPrice)) {
		alert("Unit price must be number");
		$("#addOrderDiv #unitPrice").focus();
		return false;
	}
	var intReg = /^\d+$/;
	if (!intReg.test(salesNumber)) {
		alert("Sales number must be integer");
		$("#addOrderDiv #salesNumber").focus();
		return false;
	}

	return true;
}

function calcSum() {
	var salesNumber = $("#addOrderDiv #salesNumber")[0].value;
	var unitPrice = $("#addOrderDiv #unitPrice")[0].value;
	if (salesNumber != "" && unitPrice != "") {
		var intReg = /^\d+$/;
		var floatReg = /^\d+(\.\d+)?$/;
		if (intReg.test(salesNumber) && floatReg.test(unitPrice)) {
			var money = unitPrice * salesNumber;
			$("#addOrderDiv #money")[0].value = money;
		}
	}
}

function hideAll() {
	$("#addClientDiv").hide();
	$("#clientListDiv").hide();
	$("#addOrderDiv").hide();
	$("#orderListDiv").hide();
	$("#clientDetailDiv").hide();
}
